/*
 * @Author: xiehangyun 
 * @Date: 2025-05-15 10:11:06 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-09 17:50:11
 */
import { Button } from '@nutui/nutui-react-taro';
import { View } from '@tarojs/components';
import Taro, { useDidHide, useDidShow } from '@tarojs/taro';
import { autorun, toJS } from 'mobx';
import { useEffect, useState } from 'react';
import CustomHeader from 'src/components/CustomHeader/CustomHeader';
import MyEmpty from 'src/components/MyEmpty/MyEmpty';
import MyScrollView from 'src/components/MyScrollView/MyScrollView';
import { ReceiveAddress } from 'types/type';
import AddressCard from './AddressCard/AddressCard';
import styles from './addressManage.module.less'; // 修改为模块化样式
import addressManageStore from './addressManageStore';
import BottomBtns from './BottomBtns/BottomBtns';
import userStore from 'src/pages/User/userStore';

// 地址管理
export default function AddressManage() {

    const [selectMode, setSelectMode] = useState(addressManageStore.selectMode);

    const [list, setList] = useState<Array<ReceiveAddress>>(toJS(addressManageStore.list));
    useEffect(() => autorun(() => {
        setList(toJS(addressManageStore.list));
        setSelectMode(addressManageStore.selectMode);
    }, { delay: 30 }), []);

    useDidShow(() => {
        addressManageStore.getList();
    });

    useDidHide(() => {
        //addressManageStore.setSelectMode(false);
    });

    return (
        <View className={styles.addressManage}>

            {/* 自定义头部 */}
            <CustomHeader
                showBack
                backIcon={selectMode && <Button type="primary" >确认选择</Button>}
                title={selectMode ? "选择地址" : "地址管理"}
            />

            {
                list.length > 0 &&
                <MyScrollView
                    style={{
                        "maxHeight": 'calc(100vh - 360rpx)',
                    }}
                    onScrollBottom={(e) => {

                    }}
                >
                    {
                        list.map((item, index) => {
                            return (
                                <AddressCard key={item.appAddressId} {...item} />
                            )
                        })

                    }
                </MyScrollView>
            }

            {
                list.length === 0 &&
                <MyEmpty
                    style={{
                        marginTop: '20vh'
                    }}
                    description="暂无收货地址"
                />
            }

            <BottomBtns />
        </View>
    );
}

